<?php
/**
 * 显示管理用户答题界面
 *
 * @author kinet <380511703@qq.com>
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 */
session_start();
require_once( 'path.php' );
require_once( SOJ_ROOT.'/include/function/lib.php');
require_once('Pager.php');

$tpl = new my_smarty();

//若未登录,则重定向到登录界面
if( empty($_SESSION['admin']) ){

	$_SESSION['success_message'] = 'do not login yet , '.$_SESSION['admin'];
	header( 'Location:../manager/manager_login.php' );
	exit ;
}
$request = $_GET;

if( empty( $request['name']) ){
	//用户名,直接跳转到user页面
	header( 'Location:../manager/manager_user.php' );
	exit ;
}else{
	$username = $request['name'];
}

//处理由其他页面保存在会话中的错误信息
if( !empty($_SESSION['errors']) ){

	$tpl->assign( 'errors' , $_SESSION['errors']  );
	//清除之前的错误信息
	unset( $_SESSION['errors'] );
}
$tpl->assign('admin', $_SESSION['admin']);
try{
	$db = db_connect();
	$user_topics=topic_process::get_all_by_username( $db,$username );
	$user_reply=reply_process::get_reply_topic_by_username($db,$username);
   //如果结果为空设置结果数为0以便页面显示
	//动态获得结果数组的键名
	if ( !empty($user_topics)){
	   $topic_titles = array_keys($user_topics[0] );
	   $topic_nums=count($user_topics);
		   //此配置应该可以由配置文件读出
		$params1 = array(
				'itemData' =>$user_topics,
				'perPage' => 10,
				'delta' => 8,
				'append' => true,
				'clearIfVoid' => false,
				'urlVar' => 'entrant',
				'useSessions' => true,
				'closeSession' => true,
				'mode'  => 'Jumping',
		);

		$pager1 = & Pager::factory($params1);
		$page_data1 = $pager1->getPageData();
		$links1 = $pager1->getLinks();		
	}
	else{
	
	  $topic_nums=0;
	  $topic_titles="NULL";
	  $links1="NULL";
	  $page_data1="NULL";
	}
	if(!empty($user_reply)){
	   $reply_topic_titiles = array_keys($user_reply[0] );
       $reply_nums=count($user_reply);
	   $params2 = array(
    		'itemData' =>$user_reply,
    		'perPage' => 10,
    		'delta' => 8,
    		'append' => true,
    		'clearIfVoid' => false,
    		'urlVar' => 'entrant',
    		'useSessions' => true,
    		'closeSession' => true,
    		'mode'  => 'Jumping',
		);
		$pager2 = & Pager::factory($params2);
		$page_data2 = $pager2->getPageData();
		$links2 = $pager2->getLinks();
	}
	else{
	    $reply_nums=0;
		$reply_topic_titiles="NULL";
		$links2="NULL";
		$page_data2="NULL";
	}	
    $tpl->assign( 'username' , $username );
	$tpl->assign( 'topic_titles' , $topic_titles );
	$tpl->assign( 'topic_nums' , 	$topic_nums );
	$tpl->assign( 'topics' , $page_data1 );
	$tpl->assign( 'links1' , $links1['all'] );
	$tpl->assign( 'reply_topic_titiles' , $reply_topic_titiles );
	$tpl->assign( 'reply_nums' , 	$reply_nums );
	$tpl->assign( 'reply_topics' , $page_data2 );
	$tpl->assign( 'links2' , $links2['all'] );
	$tpl->display('manager_user_topic_reply.tpl');

}catch(Exception $e){
	exception_handle( $e );
}

